
<%
	// Redirect to the home page if the user is not a manager.
	if (session.getAttribute("user_type") == null) {
		response.sendRedirect("no_permission.jsp");
	} else if (!session.getAttribute("user_type").equals("0")) {
		response.sendRedirect("index.jsp");
	}
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql.*" import="Connect.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<script language="javascript" type="text/javascript">
	function total_revenue_clicked() {
		document.bar.action = "#total_revenue";
		javascript: bar.submit();
	}
	function filter_clicked() {
		document.bar.action = "#list";
		javascript: bar.submit();
	}
</script>
</head>
<%
	// Get form data.
	String filter = request.getParameter("filter");
	String category = request.getParameter("category");
	String sort = request.getParameter("sort");
	String order = request.getParameter("order");
	if (filter == null) {
		filter = "";
	}
	if (category == null) {
		category = "owner";
	}
	if (sort == null) {
		sort = "auctions.id";
	}
	if (order == null) {
		order = "ASC";
	}

	// Prepare the sql statement.
	String query = "SELECT * FROM  auctions JOIN items ON item=items.id WHERE NOW() > end_date AND " + category + " LIKE '%" + filter
			+ "%' ORDER BY " + sort + " " + order;

	// Variable for storing total revenue.
	double total_revenue = 0.00;

	//Getting information for querying the database.
	java.sql.Connection conn = null;
	try {
		// Connect to the database.
		Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
		java.util.Properties sysprops = System.getProperties();
		sysprops.put("user", ConnectionInfo.myUserID());
		sysprops.put("password", ConnectionInfo.myPassword());
		conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

		java.sql.Statement statement = conn.createStatement();
		java.sql.ResultSet rs = statement.executeQuery(query);
%>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>View Sales Report</h1>
		<hr></hr>
		<a id="list"></a>
		<form name="bar" action="#list">
			<table class="center">
				<tr>
					<td class="center-text"><b>Search by month with YYYY-MM or by year with YYYY </b></td>
					<td class="center-text"><b>Category</b></td>
					<td class="center-text"><b>Sort By</b></td>
					<td class="center-text"><b>Order By</b></td>
					<td class="center-text"><b></b></td>
				</tr>
				<tr>
					<td><input type="text" name="filter" size="50" value="<%=filter%>"></td>
					<td><select name="category" size="1">
							<option value="owner" <%if (category.equalsIgnoreCase("owner")) {
					out.println("selected");
				}%>>Auction Owner</option>
							<option value="name" <%if (category.equalsIgnoreCase("name")) {
					out.println("selected");
				}%>>Item Name</option>
							<option value="type" <%if (category.equalsIgnoreCase("type")) {
					out.println("selected");
				}%>>Item Type</option>
							<option value="end_date" <%if (category.equalsIgnoreCase("end_date")) {
					out.println("selected");
				}%>>End Date</option>
					</select></td>
					<td><select name="sort" size="1">
							<option value="auctions.id" <%if (sort.equalsIgnoreCase("auctions.id")) {
					out.println("selected");
				}%>>Auction Id</option>
							<option value="owner" <%if (sort.equalsIgnoreCase("owner")) {
					out.println("selected");
				}%>>Auction Owner</option>
							<option value="name" <%if (sort.equalsIgnoreCase("name")) {
					out.println("selected");
				}%>>Item Name</option>
							<option value="type" <%if (sort.equalsIgnoreCase("type")) {
					out.println("selected");
				}%>>Item Type</option>
							<option value="end_date" <%if (sort.equalsIgnoreCase("end_date")) {
					out.println("selected");
				}%>>End Date</option>
					</select></td>
					<td><select name="order" size="1">
							<option value="ASC" <%if (order.equalsIgnoreCase("ASC")) {
					out.println("selected");
				}%>>Ascending</option>
							<option value="DESC" <%if (order.equalsIgnoreCase("DESC")) {
					out.println("selected");
				}%>>Descending</option>
					</select></td>
					<td><a href="javascript:void()" class="button" onclick="filter_clicked()">Filter</a></td>
					<td><a href="javascript:void()" class="button" onclick="total_revenue_clicked()">Total Revenue</a></td>
				</tr>
			</table>
		</form>
		<br />
		<%
			while (rs.next()) {
		%>
		<table class="item-table center">
			<tr>
				<th class="left-text padding-5px"><a id=<%=" " + rs.getString("id")%>>Auction Id: <%=" " + rs.getString("id")%></a></th>
				<th></th>
			</tr>
			<tr>
				<td><table class="inner-table">
						<tr>
							<th>Item Name:</th>
							<td><%=rs.getString("name")%></td>
						</tr>
						<tr>
							<th>Item Type:</th>
							<td><%=rs.getString("type")%></td>
						</tr>
						<tr>
							<th>Item Year:</th>
							<td><%=rs.getString("year")%></td>
						</tr>
						<tr>
							<th>Item Description:</th>
							<td><%=rs.getString("description")%></td>
						</tr>
						<tr>
							<th>Current Bid:</th>
							<td><b> <%
 	// Calcualte the current bid
 			java.sql.Connection conn2 = null;
 			conn2 = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
 			java.sql.Statement statement2 = conn2.createStatement();
 			// Get current bids.
 			java.sql.ResultSet rs2 = statement2.executeQuery("SELECT * FROM bids WHERE auction_id='" + rs.getString("auctions.id")
 					+ "' ORDER BY auction_id DESC, bid_price DESC, bid_time ASC");
 			String current_bid = "0.00";
 			if (rs2.next()) {
 				double highest_bid = Double.parseDouble(rs2.getString("bid_price"));
 				String highest_bidder = rs2.getString("bidder_username");
 				boolean next_bid = rs2.next();
 				if (next_bid == false) {
 					out.println(rs.getString("minimum_bid"));
 					highest_bid = Double.parseDouble(rs.getString("minimum_bid"));
 					current_bid = ""
 							+ ConnectionInfo.twoSigFigs(Double.parseDouble(rs.getString("minimum_bid"))
 									+ Double.parseDouble(rs.getString("bid_increment")));
 				} else {
 					double next_highest_bid = Double.parseDouble(rs2.getString("bid_price"));
 					if (next_highest_bid < highest_bid) {
 						highest_bid = next_highest_bid + Double.parseDouble(rs.getString("bid_increment"));
 					}

 					// Format string for printing.
 					String highest_bid_string = "" + ConnectionInfo.twoSigFigs(highest_bid);
 					current_bid = "" + ConnectionInfo.twoSigFigs(highest_bid + Double.parseDouble(rs.getString("bid_increment")));
 					int length = highest_bid_string.length();
 					int index = highest_bid_string.indexOf('.');
 					while (index > length - 3) {
 						highest_bid_string += "0";
 						length = highest_bid_string.length();
 					}
 					if (index < length - 3) {
 						highest_bid_string = highest_bid_string.substring(0, index + 3);
 					}
 					out.println(highest_bid_string);
 				}
 				// Tally the total revenue
 				total_revenue += highest_bid;
 			}
 %>
							</b></td>
						</tr>
						<tr>
							<th>Minimum Bid:</th>
							<td><%=" " + rs.getString("minimum_bid")%></td>
						</tr>
						<tr>
							<th>Bid Increment:</th>
							<td><%=" " + rs.getString("bid_increment")%></td>
						</tr>
						<tr>
							<th>Open Date:</th>
							<td><%=" " + rs.getString("open_date")%></td>
						</tr>
						<tr>
							<th>End Date:</th>
							<td><%=" " + rs.getString("end_date")%></td>
						</tr>
						<tr>
							<th>Owner:</th>
							<td><%=" " + rs.getString("owner")%></td>
						</tr>
					</table></td>
				<td></td>
			</tr>
		</table>
		<br />
		<%
			}
				// Format string for printing.
				String total_revenue_string = "" + ConnectionInfo.twoSigFigs(total_revenue);
				int length = total_revenue_string.length();
				int index = total_revenue_string.indexOf('.');
				while (index > length - 3) {
					total_revenue_string += "0";
					length = total_revenue_string.length();
				}
				if (index < length - 3) {
					total_revenue_string = total_revenue_string.substring(0, index + 3);
				}
		%>
		<a id="total_revenue"></a>
		<table class="center item-table">
			<tr>
				<th>Total Revenue Generated By This Subset:</th>
				<td style="width: 75%; padding-left: 10px;">$<%=total_revenue_string%></td>
			</tr>
			<tr>
				<th>10% Earned By This Subset:</th>
				<td style="width: 75%; padding-left: 10px;">$<%=ConnectionInfo.twoSigFigs(Double.valueOf(total_revenue_string)*0.10)%></td>
			</tr>
		</table>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>
<%
	} catch (Exception e) {
		e.printStackTrace();
		out.print(e.toString());
	} finally {
		try {
			conn.close();
		} catch (Exception ee) {
		}
	}
%>